package com.dw.widget.slideshow.config;

import java.util.List;

import com.dw.widget.slideshow.config.view.HeaderView;
import com.dw.widget.slideshow.config.view.HeaderView.OnBackButtonClickListener;
import com.dw.widget.slideshow.config.view.list.ListBlockView;
import com.dw.widget.slideshow.config.view.list.ListTextItemView;
import com.dw.widget.slideshow.db.helper.ImageDBHelper;
import com.dw.widget.slideshow.db.helper.ScanFolderHelper;
import com.dw.widget.slideshow.db.object.ImageData;
import com.dw.widget.slideshow.db.object.ScanFolderData;
import com.dw.widget.slideshow.utils.ApplicationEx;
import com.dw.widget.slideshow.utils.db.DataObject;
import com.dw.widget.slideshow.utils.log.Log;
import com.dw.widget.slideshow.R;
import com.haarman.listviewanimations.itemmanipulation.OnDismissCallback;
import com.haarman.listviewanimations.itemmanipulation.SwipeDismissAdapter;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;

public class FolderListActivity extends Activity {

	private final ApplicationEx _APP = ApplicationEx.INSTANCE();
	private final String TAG = "FolderListActivity";
	
	private HeaderView headerView;
	private ListBlockView listBlockView;
	private Button bnOk;
	
	private List<DataObject> folders = null;
	private ScanFolderHelper dbHelper = null;
	
	private final int ACTIVITY_REQ_CODE = 0x8D90112;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.folder_list);
		dbHelper = new ScanFolderHelper(FolderListActivity.this, _APP.DB_NAME, ScanFolderData.class);
		
		findViewsById();
		initViews();
	}
	
	private void findViewsById()
	{
		headerView = (HeaderView)findViewById(R.id.folderlist_header);
		listBlockView = (ListBlockView)findViewById(R.id.folderlist_block);
		bnOk = (Button)findViewById(R.id.folderlist_bnOk);
	}
	
	private void initViews()
	{
		// 设置滑动删除Adapter
		SwipeDismissAdapter adapter = new SwipeDismissAdapter(listBlockView.getAdapter(), new OnFolderDismissCallback());
		adapter.setListView(listBlockView);
		listBlockView.setAdapter(adapter);
		
		headerView.setOnBackButtonClickListener(new OnBackButtonClickListener() {
			
			@Override
			public void onButtonClick(View v) {
				
				FolderListActivity.this.finish();
			}
		});
		
		bnOk.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
				Intent intent = new Intent(FolderListActivity.this, ChooseFolderActivity.class);
				
				String sdPath = Environment.getExternalStorageDirectory().getAbsolutePath();
				intent.putExtra(_APP.INTENT_MSG_FULLPATH, sdPath);
				FolderListActivity.this.startActivityForResult(intent, ACTIVITY_REQ_CODE);
			}
		});
		
		// 从数据库中读取文件夹列表
		showSelectedFolders();
	}

	private class OnFolderDismissCallback implements OnDismissCallback{

		@Override
		public void onDismiss(ListView arg0, int[] arg1) {
			
			if(folders == null || folders.size() <= 0)
			{
				return;
			}
			
			for(int i : arg1)
			{
				// 从数据库中删除
				ScanFolderData data = (ScanFolderData)folders.get(i);
				dbHelper.delete(data);
				
				// delete files
				ImageDBHelper imageDbHelper = new ImageDBHelper(FolderListActivity.this, _APP.DB_NAME, ImageData.class);
				String sql = String.format("DELETE FROM %1$s WHERE folder LIKE '%2$s%%'", 
							imageDbHelper.getTableName(), data.getFolder());
				
				Log.d(FolderListActivity.this, TAG, String.format("delete sql: %1$s", sql));
				imageDbHelper.getWritableDatabase().execSQL(sql);
				imageDbHelper.getWritableDatabase().close();
				
				folders.remove(i);
				
				listBlockView.removeListItem(i);
			}
		}
		
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {

		super.onActivityResult(requestCode, resultCode, data);
		showSelectedFolders();
	}
	
	/**
	 * 显示选中的文件夹列表
	 */
	private void showSelectedFolders()
	{
		listBlockView.clear();
		
		ScanFolderData condition = new ScanFolderData();
		condition.setSource(_APP.SOURCE_FOLDER);
		folders = dbHelper.query(condition);
		for(DataObject dataObj : folders)
		{
			ScanFolderData folder = (ScanFolderData)dataObj;
			String folderName = folder.getName();
			String fullPath = folder.getFolder();
			
			getLayoutInflater();
			ListTextItemView item = (ListTextItemView)LayoutInflater.from(this).inflate(R.layout.listitem_text, null);
			item.setIcon(R.drawable.icon_document01);
			item.setText1(folderName);
			item.setText2(fullPath);
			item.setBackgroundColor(0xFFEBEBEB);
			
			listBlockView.addListItem(item);
		}
	}
}
